<script>
    import {onMount} from "svelte";
    import FullPathData from "../../eventbus/FullPathData.js";
    import SaveManager from "../../scripts/save/SaveManager.js";
    import {mapEventBus} from "../../eventbus/map_signal.js";
    import Toast from "./Toast.svelte";

    export let  where = {
        col: 0,
        row: 0
    }
    export let  item = {
        text: "显示",
        callback: ()=>{

        },
        cost: 2,
        description: ' ',
        disable_cost: false
    }
    onMount(
        ()=>{
            FullPathData.target = where
        }
    )
    function run(item){
        if (item.disable_cost !== true){
            // 当动作没有取消消耗点的策略时 消费对应行动点
            let save = new SaveManager(window)
            let resources = save.load_data('resource')
            if(resources[save.load_data('play').power].runpoint  < item.cost){
                mapEventBus.emit('show-toast', {
                    message: '行动力不足！可以点击下一回合重置行动力。',
                    type: 'error'
                });
                return false
            }
            resources[save.load_data('play').power].runpoint -= item.cost
            save.save_data('resource', resources)
            mapEventBus.emit('resource-update', Math.random())
        }
        item.callback()
    }
</script>

<div class="list" on:click={()=>{run(item)}}>
    <div class="title">{item.text}</div>
    <div class="description">
        <div class="cost">行动力消耗: 【{item.cost | 0}】</div>
        {@html item.description}
    </div>
</div>

<style>
    .cost{
        font-size: 12px;
        color: blue;
    }
    .list{
        /*margin: 10px 0;*/
        width: 100%;
        padding: 10px 6px;
        box-sizing: border-box;
        cursor: pointer;
        user-select: none;
        &:hover{
            background: #e0e0e0;
        }
    }
    .description{
        font-size: 12px;
        margin: 4px 0;
        color: slategray;
    }
</style>